package engine.core
{
	import engine.math.*;
	import flash.display.*;
	import flash.events.*;
	
	public class RxMouse
	{
		private static var s_eventObject:InteractiveObject = null;
		private static var s_stage:Stage;
		private static var s_focus:Boolean = false;
		private static var s_pos:RxVector4 = new RxVector4();
		private static var s_deltaX:Number = 0;
		private static var s_deltaY:Number = 0;
		private static var s_clickX:Number = 0;
		private static var s_clickY:Number = 0;
		private static var s_leftDown:Boolean = false;
		private static var s_leftClicked:Boolean = false;
		private static var s_dragAngle:Number = 0;
		private static var s_dragLength:Number = 0;
		private static var s_wheel:Number = 0;
		private static var s_useDebugState:Boolean = false;
		
		public function RxMouse()
		{
		}
		
		public static function Initialize(eventObject:InteractiveObject, stage:Stage) : void
		{
			s_eventObject = eventObject;
			s_stage = stage;
			s_eventObject.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			s_eventObject.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			s_eventObject.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			s_eventObject.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			s_eventObject.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
		}
		
		public static function GameWorldHasMouseFocus() : Boolean
		{
			return s_focus;
		}
		
		private static function onMouseOver(event:MouseEvent) : void
		{
			if (s_useDebugState)
			{
				return;
			}
			s_focus = true;
		}
		
		private static function onMouseOut(event:MouseEvent) : void
		{
			if (s_useDebugState)
			{
				return;
			}
			s_focus = false;
		}
		
		private static function onMouseMove(event:MouseEvent) : void
		{
			var _loc_2:Number = NaN;
			var _loc_3:Number = NaN;
			if (s_useDebugState)
			{
				return;
			}
			s_deltaX = event.stageX - s_pos.x;
			s_deltaY = event.stageY - s_pos.z;
			s_pos.x = event.stageX;
			s_pos.z = event.stageY;
			if (s_leftDown)
			{
				_loc_2 = s_pos.x - s_clickX;
				_loc_3 = s_pos.z - s_clickY;
				s_dragAngle = Math.atan2(_loc_3, _loc_2);
				s_dragLength = Math.sqrt(_loc_2 * _loc_2 + _loc_3 * _loc_3);
			}
		}
		
		public static function onMouseDown(event:MouseEvent) : void
		{
			s_leftClicked = true;
			s_clickX = event.stageX;
			s_clickY = event.stageY;
			AddMouseDownResponseListeners();
			s_leftDown = true;
		}
		
		private static function AddMouseDownResponseListeners() : void
		{
			s_stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			s_stage.addEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private static function RemoveMouseDownResponseListeners() : void
		{
			s_stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			s_stage.removeEventListener(MouseEvent.CLICK, onMouseClick);
		}
		
		private static function onMouseUp(event:MouseEvent) : void
		{
			s_leftDown = false;
			s_dragAngle = 0;
			s_dragLength = 0;
			RemoveMouseDownResponseListeners();
		}
		
		private static function onMouseClick(event:MouseEvent) : void
		{
			RemoveMouseDownResponseListeners();
		}
		
		private static function onMouseWheel(event:MouseEvent) : void
		{
			s_wheel = event.delta;
		}
		
		public static function Reset() : void
		{
			s_leftClicked = false;
			s_deltaX = 0;
			s_deltaY = 0;
			s_wheel = 0;
		}
		
		public static function SetPlaybackValues(param1:Number, param2:Number, param3:Boolean, param4:Boolean) : void
		{
			s_pos.x = param1;
			s_pos.z = param2;
			s_leftClicked = param3;
			s_leftDown = param4;
		}
		
		public static function get x() : int
		{
			return s_pos.x;
		}
		
		public static function get y() : int
		{
			return s_pos.z;
		}
		
		public static function get pos() : RxVector4
		{
			return s_pos;
		}
		
		public static function get deltaX() : int
		{
			return s_deltaX;
		}
		
		public static function get deltaY() : int
		{
			return s_deltaY;
		}
		
		public static function get leftDown() : Boolean
		{
			return s_leftDown;
		}
		
		public static function get leftClicked() : Boolean
		{
			return s_leftClicked;
		}
		
		public static function get wheel() : Number
		{
			return s_wheel;
		}
		
		public static function getDebugState() : Object
		{
			var _loc_1:* = new Object();
			_loc_1.leftDown = s_leftDown;
			_loc_1.pos = s_pos;
			_loc_1.clickX = s_clickX;
			_loc_1.clickY = s_clickY;
			_loc_1.leftClicked = s_leftClicked;
			_loc_1.focus = s_focus;
			_loc_1.deltaX = s_deltaX;
			_loc_1.deltaY = s_deltaY;
			return _loc_1;
		}
		
		public static function setDebugState(param1:Object) : void
		{
			s_useDebugState = true;
			s_leftDown = param1.leftDown;
			s_pos = param1.pos;
			s_clickX = param1.clickX;
			s_clickY = param1.clickY;
			s_leftClicked = param1.leftClicked;
			s_focus = param1.focus;
			s_deltaX = param1.deltaX;
			s_deltaY = param1.deltaY;
			s_dragAngle = 0;
			s_dragLength = 0;
			s_wheel = 0;
		}
	}
}